# -*- coding: euc-kr -*-
import time
import os
import shutil


versionInfoFile = "D:\\EDDA_PatchUpload\\EU_TestNew_Clone.ver"
targetDir = "D:\\EDDA_PatchUpload\\EU_TestNew_Clone\\"

createFileName = "Resource info.txt"
createFolderName = [
    "CWorking"
,   "SWorking"
]


#===============================================================================
def getVersionFromDate():
    today = time.localtime()
    
    return '{:04}{:02}{:02}'.format(today.tm_year%100, today.tm_mon, today.tm_mday)

def makeNewVersion(version):
    postFix = 0
    today = int(getVersionFromDate())
    curDay = int(version[:6])
    
    if (today == curDay):
        postFix = int(version[7:]) + 1
    else:
        postFix = 1

    newVersion = '{:06}_{:02}'.format(today, postFix)
    
    return newVersion

def readVersion(strVersionFile):
    try:
        fd = open(strVersionFile, "r")
    except:
        return
    
    strVersion = fd.readline()
    
    fd.close()
    
    return strVersion[:(len(strVersion))]

def writeVersion(strVersionFile, newVersion):
    fd = open(strVersionFile, "w")
    
    fd.write(newVersion)
    
    fd.close()

    return


#===============================================================================
def svnUpdate(targetDir):
    os.system("svn update " + targetDir)

def svnAdd(targetDir):
    os.system("svn add " + targetDir)

def svnCommit(targetDir, message):
    os.system("svn commit " + targetDir + " --message " + message)


#===============================================================================
def makeDir(targetDir):
    os.mkdir(targetDir)

    for dirName in createFolderName:
        target = os.path.join(targetDir, dirName)
        os.mkdir(target)

def makeFile(targetDir):
    target = os.path.join(targetDir, createFileName)

    try:
        fd = open(target, "w")
        fd.close()
    except:
        pass


#===============================================================================
def main():
    try:
        svnUpdate(targetDir)

        ver = makeNewVersion(readVersion(versionInfoFile))

        target = os.path.join(targetDir, ver)

        makeDir(target)
        makeFile(target)

        svnAdd(target)
        svnCommit(targetDir, ver)

        writeVersion(versionInfoFile, ver)
        
    except:
        return

    return


#===============================================================================
if __name__ == "__main__":
    main()
    pass
